package com.example.kkblog.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.kkblog.domain.Article;
import com.example.kkblog.domain.Dynamic;
import com.example.kkblog.domain.dto.ArticleDetailDto;
import com.example.kkblog.domain.dto.ArticlePreDto;
import com.example.kkblog.domain.dto.DynamicPreDto;
import com.example.kkblog.domain.query.ArticleQuery;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author xiaoke
 * @date 2024/4/29
 */
@Mapper
@Repository
public interface ArticleMapper extends BaseMapper<Article> {
    @Select("SELECT a.*,COUNT(c.id) as commentNumber FROM  " +
            "(SELECT d.*, u.nickname as nickname, u.avatar as avatar FROM `article` d, `user` u  " +
            "WHERE d.user_id=u.id and d.status=0 and d.id=#{id}) as a " +
            "LEFT JOIN article_comment c " +
            "ON a.id = c.article_id " +
            "GROUP BY a.id")
    ArticleDetailDto selectDetailById(String id);

    @Update("UPDATE `article` SET `view_number`=`view_number` + 1 WHERE id=#{id}")
    void addViewById(String id);
    @Update("UPDATE `article` SET `like_number`=`like_number` + 1 WHERE id=#{id}")
    void addLikeById(Integer id);
    @Update("UPDATE `article` SET `like_number`=`like_number` - 1 WHERE id=#{id}")
    void cancelLikeById(Integer id);

    // 删除文章
    @Update("UPDATE `article` SET status=2 WHERE id=#{id}")
    Integer deleteArticleById(Integer id);

    @Select("SELECT a.*,COUNT(c.id) as commentNumber FROM   " +
            "(SELECT d.*,u.nickname as nickname, u.avatar as avatar FROM `article` d, `user` u   " +
            "WHERE d.user_id=u.id and d.status=0 ORDER BY d.id DESC LIMIT 8) as a  " +
            "LEFT JOIN article_comment c  " +
            "ON a.id = c.article_id  " +
            "GROUP BY a.id " +
            "ORDER BY a.id DESC")
    List<ArticlePreDto> selectNewestArticles();

    @Select("SELECT a.*,COUNT(c.id) as commentNumber FROM   " +
            "(SELECT d.*, u.nickname as nickname, u.avatar as avatar FROM `article` d,`user` u   " +
            "WHERE d.user_id=u.id and d.status=0 and d.id < #{id} ORDER BY d.id DESC LIMIT 8) as a  " +
            "LEFT JOIN article_comment c  " +
            "ON a.id = c.article_id  " +
            "GROUP BY a.id " +
            "ORDER BY a.id DESC")
    List<ArticlePreDto> selectMoreNewestArticles(Integer id);

    // 根据用户id获取文章
    @Select("SELECT a.*,COUNT(c.id) as commentNumber FROM   " +
            "(SELECT d.*, u.nickname as nickname, u.avatar as avatar FROM `article` d,`user` u " +
            "WHERE  d.user_id=u.id and d.status=0 and d.user_id=#{id} ORDER BY d.id DESC LIMIT 5) as a  " +
            "LEFT JOIN article_comment c  " +
            "ON a.id = c.article_id  " +
            "GROUP BY a.id " +
            "ORDER BY a.id DESC")
    List<ArticlePreDto> selectByUserId(Integer id);

    // 根据用户id查询更多文章
    @Select("SELECT a.*,COUNT(c.id) as commentNumber FROM   " +
            "(SELECT d.*, u.nickname as nickname, u.avatar as avatar FROM `article` d,`user` u " +
            "WHERE  d.user_id=u.id and d.status=0 and d.user_id=#{userId} and and d.id < #{id} ORDER BY d.id DESC LIMIT 5) as a  " +
            "LEFT JOIN article_comment c  " +
            "ON a.id = c.article_id  " +
            "GROUP BY a.id " +
            "ORDER BY a.id DESC")
    List<ArticlePreDto> selectMoreByUserId(Article article);
    
    // 搜索文章
    @Select("SELECT a.*, u.nickname as nickname, u.avatar as avatar " +
            "FROM article a, `user` u " +
            "WHERE a.user_id = u.id and a.status = 0 AND a.id > #{lastId} AND (a.title LIKE CONCAT('%',#{key},'%') or a.preview LIKE CONCAT('%',#{key},'%')) " +
            "ORDER BY a.id ASC " +
            "LIMIT #{pageSize}")
    List<ArticlePreDto> selectArticlesByKey(ArticleQuery articleQuery);
}
